06. Error Handling

Error Handling

Error Handling Intro

FSND C2 L3 A15 Error Handling Part 1

FSND C2 L3 A16 Error Handling Part 2

FSND C2 L3 A17 Error Handling Part 3

When you use the abort method, the default response is not digestible for the client or user.

abort(404)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>

In addition, we want to ensure all of our server responses have consistent formatting and that we provide adequate information to the client regarding the error. The @app.errorhandler decorator allows you to specify the behavior for expected errors.
When using this decorator take into consideration:

  • passing the status code or Python error as an argument to the decorator
  • logical naming of the function handler
  • consistent formatting and messaging of the JSON object response
@app.errorhandler(404)
def not_found(error):
    return jsonify({
        "success": False, 
        "error": 404,
        "message": "Not found"
        }), 404

Calls to abort automatically return errors in what data format?

SOLUTION: HTML

What's missing from or wrong with the following code?

  @app.errorhandler()
  def unprocessable(error):
    return jsonify({
      "success": False, 
      "error": 422,
      "message": "unprocessable"
      }), 422
SOLUTION: `app.errorhandler` is missing an argument, namely the status code

What's missing from or wrong with the following code?

  @app.errorhandler(422)
  def unprocessable(error):
    return jsonify({
      "success": False, 
      "message": "unprocessable"
      })
SOLUTION: The error code needs to be returned to the response

What's missing from or wrong with the following code?

  @app.errorhandler(422)
  def unprocessable(error):
    return jsonify({
      "success": False, 
      "error": 422,
      }), 422
SOLUTION: A message is missing from the response body